#ifndef _NE_MANAGER_H_
#define _NE_MANAGER_H_

#include <common/Singleton.h>

namespace ne
{
    template<typename TManagerType, typename TKeyType, typename TValueType>
    class Manager : public Singleton<TManagerType>
    {
        public:
            typedef std::map<TKeyType, TValueType*> Map;
            typedef typename Map::iterator Iterator;
            typedef typename Map::const_iterator ConstIterator;

        public:
            Manager()
            {
            }

            virtual ~Manager()
            {
                _destroyAllValues();
            }

        protected:
            bool _addKeyValue(const TKeyType &key, TValueType *pValue)
            {
                if (mKeyValueMap.find(key) == mKeyValueMap.end())
                {
                    mKeyValueMap[key] = pValue;
                    return true;
                }
                return false;
            }

            TValueType* _getValue(const TKeyType &key) const
            {
                ConstIterator it = mKeyValueMap.find(key);
                if (it != mKeyValueMap.end())
                {
                    return it->second;
                }
                return 0;
            }

            size_t _getNumValue() const
            {
                return mKeyValueMap.size();
            }

            bool _hasKey(const TKeyType &key) const
            {
                if (mKeyValueMap.find(key) != mKeyValueMap.end())
                {
                    return true;
                }
                return false;
            }

            bool _destroyValue(const TKeyType &key)
            {
                Iterator it = mKeyValueMap.find(key);
                if (it != mKeyValueMap.end())
                {
                    delete it->second;
                    mKeyValueMap.erase(it);
                    return true;
                }
                return false;
            }

            void _destroyAllValues()
            {
                Iterator it, iend;
                iend = mKeyValueMap.end();
                for (it=mKeyValueMap.begin(); it!=iend; ++it)
                {
                    delete it->second;
                }
                mKeyValueMap.clear();
            }

        private:
            Manager(const Manager &);

            Manager& operator = (const Manager &);

        protected:
            Map mKeyValueMap;
    };
}

#endif
